package com.gen.info.generate_cluster_information.util;

import com.gen.info.generate_cluster_information.mode.*;

import java.util.ArrayList;
import java.util.List;

public class OceanBaseSqlObjectGenerateUtil {
    /*创建资源规格的sql*/
    public static String getUnitSql(RSUnitVO rsUnitVO){
        String create_unit_sql="CREATE resource unit IF NOT EXISTS "+rsUnitVO.getUnitName()+
                " max_cpu='"+rsUnitVO.getCpu()+"',min_cpu='"+rsUnitVO.getCpu()+
                "', max_memory='"+rsUnitVO.getMemory()+"G', min_memory='"+rsUnitVO.getMemory()+"G'," +
                " max_iops=5000, min_iops=128,max_session_num=75,max_disk_size='500G'";
        return create_unit_sql;
    }
    /*创建资源池sql*/
    public static String getRSPoolSql(RSPoolVO rsPoolVO){
        StringBuilder zone_list = new StringBuilder();
         int zone_num = rsPoolVO.getZonelist().size();
        for (int i = 1; i <= zone_num; i++) {
            if (i==zone_num) zone_list.append("'zone"+i+"'");
             else     zone_list.append("'zone"+i+"',");
        }
        String create_rspool_sql="CREATE RESOURCE POOL IF NOT EXISTS "+rsPoolVO.getPoolName()+
                " UNIT '"+rsPoolVO.getRsUnitVO().getUnitName()+"', UNIT_NUM "+rsPoolVO.getUnitNum()
                +", ZONE_LIST ("+zone_list+")";
        return create_rspool_sql;
    }
    /*创建租户的sql*/
    public static  String getTenantSql(TenantVO tenantVO){
        String create_tenant_sql="CREATE TENANT IF NOT EXISTS "+tenantVO.getTenantName()+
                " PRIMARY_ZONE='"+tenantVO.getPrimaryZone()+"', RESOURCE_POOL_LIST=('"+tenantVO.getResourcePools()
                +"'), CHARSET='utf8mb4' SET ob_tcp_invited_nodes TO '%',ob_compatibility_mode= '"+tenantVO.getDbtype()+"'";
        return create_tenant_sql;
    }
    /*创建数据库*/
    public static ArrayList<String> getDataBaseSql(ArrayList<String> databaseList){
        final ArrayList<String> sql_list = new ArrayList<>();
        for (String db : databaseList) {
            sql_list.add("CREATE DATABASE IF NOT EXISTS  "+db+";");
        }
        return sql_list;
    }
    /*创建用户*/
    public static ArrayList<String> getUserSql(ArrayList<UserInfo> users){
        final ArrayList<String> sql_list = new ArrayList<>();
        for (UserInfo user : users) {
            sql_list.add("CREATE USER IF NOT EXISTS "+user.getUsername()+" IDENTIFIED BY \""+user.getPassword()+"\"");
        }
        return sql_list;
    }
    /*创建只读用户*/
    public static String getReadOnlyUserSql(){
        String sql =    "CREATE USER IF NOT EXISTS readonly IDENTIFIED BY \"aaAA11__\"";
        return sql;
    }
    /*授权用户*/
    public static ArrayList<String> getAuthSql(ArrayList<UserDataBaseInfo> auth, List<String> common_db){
        final String authList = CommonUtil.getAuthList(common_db);
        final ArrayList<String> sql_list = new ArrayList<>();
        for (UserDataBaseInfo info : auth) {
            sql_list.add("Grant "+authList+" on "+info.getDatabase()+".* to "+info.getUsername());
        }
        return sql_list;
    }
}
